/*=============================================================================

  SYSTEM MANAGER MODULE

  FILE: SYSmgr.h
  
  DESCRIPTION

    Manager Module Definition

  EXTERNALIZED FUNCTIONS

    
  Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

                             EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when       who     what, where, why
  ---------  -------  -------------------------------------------------------
   06/28/03   nso4th   Version 0.1

=============================================================================*/

#ifndef _SYSMGR_H_
#define _SYSMGR_H_

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include <Cust_AppDef.h>

#include <SYSmgr_EVT.h>
#include <SYSmgr_STATE.h>
#include <SYSmgr_CMD.h>
#include <SYSmgr_DEV.h>
#include <SYSmgr_CFG.h>
#include <SYSmgr_INI.h>

/*=============================================================================
  SYSmgr CLASS DEFINITION
=============================================================================*/
class SYSmgr : public mgrBase
{
public:
	SYSmgr();
	~SYSmgr();

public:
	virtual boolean Init(void);
	virtual boolean Close(void);
	boolean Stop(void);

/*===============================================
  APPLICATION MODULE
===============================================*/
protected:
	void APP_Alloc();
	void APP_Free();

public:
	// RESET
	boolean RESET_start(void);
	boolean RESET_clear(void);
	boolean HOME_start(dword deAxis);
	
	// AUTO
	boolean AUTO_start(void);
	
	// MANUAL
	boolean MANUAL_start(dword manMode);
	dword MANUAL_current(void);
	boolean MANUAL_setCYCLE(int32 total);
	int32 MANUAL_getCYCLE(void);
	
protected:
	void WATCH_reset(void);
	double64 WATCH_get(void);

protected:
	dword 	m_manMode, m_homeReq;
	int32	m_cycleCount, m_cycleTotal;
	mcxt	m_sysWatch;
	
/*---------------------------------------------------------
SIGNAL TOWER & OP BUTTON LAMP
---------------------------------------------------------*/
protected:
	void SIGT_STATE(dword majS, dword minS);
public:
	void SIGT_STATE_ON(dword sigT);

/*---------------------------------------------------------
  ERROR HANDLING
---------------------------------------------------------*/
protected:
	void ERR_REPORT(const char *title, const char *msg);
	void NOTI_REPORT(const char *title, const char *msg);


/*---------------------------------------------------------
  CONFIGURATION
---------------------------------------------------------*/
public:
	boolean CFG_Setup(char *cfgName);
	boolean CFG_Save(char *cfgName);

	SYSmgr_cfg_s *CFG_Ptr(void);
	boolean CFG_Copy(SYSmgr_cfg_s *cfg);
	boolean CFG_Set(SYSmgr_cfg_s *cfg);

protected:
	void CFG_Alloc(void);
	void CFG_Free(void);
	
protected:
	SYSmgr_cfg_s			*m_cfg;
	
//-------------------------------------------------
// DEVICE CONTROL
//-------------------------------------------------
public:
	boolean DEV_Init(void);
	boolean DEV_Close(void);
	boolean DEV_Reset(void);

protected:
	void DEV_Alloc(void);
	void DEV_Free(void);

	boolean	m_devReset;

/*---------------------------------------------------------
  SUB-MANAGER INTERFACE
---------------------------------------------------------*/
protected:
	boolean MGR_Init(void);
	boolean MGR_Close(void);
	boolean MGR_Lock(boolean lock);
	
	void MGR_Alloc(void);
	void MGR_Free(void);

	BKmgr		*m_bkMgr;
	IOmgr		*m_ioMgr;

/*---------------------------------------------------------
  APPLICATION MODULE
---------------------------------------------------------*/
protected:
	boolean MOD_Init(void);
	boolean MOD_Close(void);

protected:
	mcxt	m_areaMod;
	boolean	m_areaInit;

	void MOD_Alloc(void);
	void MOD_Free(void);

public:
	void MOD_ERRREPORT(dword errCode);
/*---------------------------------------------------------
  APPLICATION EVENT HANDLER
---------------------------------------------------------*/
protected:
	void EVT_FG(dword evt, dword wp, dword lp);
	void EVT_IOmgr(dword evt, dword wp, dword lp);
	
protected:
	void EVT_Alloc(void);
	void EVT_Free(void);
	
/*===============================================
  DEVICE CONTROL MODULE
===============================================*/
/*---------------------------------------------------------
  DIGITAL IO INTERFACE
---------------------------------------------------------*/
public:
	boolean HIO_Setup(char *pszFile);

protected:
	boolean HIO_Init(void);
	boolean HIO_Close(void);

protected:
	HIO				*m_pIO;
	boolean			m_ioInit;
	char				*m_ioCFG;

	void HIO_Alloc(void);
	void HIO_Free(void);

/*---------------------------------------------------------
  ANALOG IO INTERFACE
---------------------------------------------------------*/
public:
	boolean HAIO_Setup(char *pszFile);

protected:
	boolean HAIO_Init(void);
	boolean HAIO_Close(void);

protected:
	HAIO			*m_pAIO;
	boolean			m_aioInit;
	char				*m_aioCFG;

	void HAIO_Alloc(void);
	void HAIO_Free(void);
	
/*---------------------------------------------------------
  MOTION CONTROL
---------------------------------------------------------*/
public:
	boolean HMot_Setup(char *pszFile);

	boolean HMot_StopAll(void);
	boolean HMot_IsMoveDoneAll(void);
	boolean HMot_IsMoveDone(int32 axID, double64 pos);
	boolean HMot_IsExactPosition(int32 axID, double64 pos);
	boolean HMot_Move(int32 axID, double64 pos, double64 vel);

	// Pre-Defined Position & Velocity
	double64 HMot_TblPosition(int32 tblID);
	double64 HMot_TblVelocity(int32 tblID);

	boolean HMot_TrigDisable(void);
	boolean HMot_TrigSet(boolean enable);

	//E-Gear
	boolean HMot_EgearEnable(int32 axID, boolean enable);
	boolean HMot_IsEgear(int32 axID);

	boolean HMot_SetPosition(int32 axID, double64 dPos);
	boolean HMot_GetPosition(int32 axID, double64 *dPos);

	boolean HMot_GetVelocity(int32 axID, double64 *pVel);

	HMot			*m_pMot;

protected:
	boolean HMot_Init(void);
	boolean HMot_Close(void);

protected:
	boolean			m_motInit;
	char				*m_motCFG;

	void HMot_Alloc(void);
	void HMot_Free(void);

	
/*---------------------------------------------------------
  FRAME GRABBER INTERFACE
---------------------------------------------------------*/
public:
	boolean HFg_GrabSync(boolean bCont);
	boolean HFg_GrabSync1(void);
	boolean HFg_GrabASync(boolean bCont);
	boolean HFg_GrabStop(void);
	boolean HFg_TakeShot(void);
	HFgDev		*m_pFg;

protected:
	boolean		m_fgInit;
	char		*m_fgCFG;

	void HFg_Alloc(void);
	void HFg_Free(void);

	boolean HFg_Setup(char *pszFile);
	boolean HFg_Init(void);
	boolean HFg_Close(void);

/*---------------------------------------------------------
  CAMERA INTERFACE
---------------------------------------------------------*/
public:

	HCAM		*m_pCam;
protected:
	boolean		m_camInit;
	char			*m_camCFG;

	void HCam_Alloc(void);
	void HCam_Free(void);

	boolean HCam_Setup(char *pszFile);
	boolean HCam_Init(void);
	boolean HCam_Close(void);
	
/*---------------------------------------------------------
  STATE MACHINE
---------------------------------------------------------*/
protected:
	dword STATE_MACHINE(dword dwType, dword dwEvent, dword wParam, dword lParam);
	
	dword			m_mjPrev, m_mnPrev[MGR_MAX_MAJSTATE];
	
	dword State_MajPrevious(void);
	dword State_MinPrevious(void);

	void STATE_POLLING(void);

/*---------------------------------------------------------
  MONITORING
---------------------------------------------------------*/
protected:
	void MON_STATE(void);
	boolean MON_Init(void);
	boolean MON_Close(void);
	void MON_LOCK(boolean lock);
	boolean MON_IsLock(void);

protected:
	void MON_Alloc(void);
	void MON_Free(void);

	boolean	m_monLock;
	int32	m_monCnt;

//*****  IDL STATE  *****
protected:
	dword IDL_STATE(dword dwType, dword dwEvent, dword wParam, dword lParam);
	dword IDL_ProcIDL(dword dwType, dword dwEvent, dword wParam, dword lParam);
	dword IDL_ProcERR(dword dwType, dword dwEvent, dword wParam, dword lParam);

//*****  DEV STATE  *****
protected:
	dword DEV_STATE(dword dwType, dword dwEvent, dword wParam, dword lParam);
	dword DEV_ProcIDL(dword dwType, dword dwEvent, dword wParam, dword lParam);
	dword DEV_ProcINIT(dword dwType, dword dwEvent, dword wParam, dword lParam);
	dword DEV_ProcCLOSE(dword dwType, dword dwEvent, dword wParam, dword lParam);

//*****  RESET STATE  *****
protected:
	dword RESET_STATE(dword dwType, dword dwEvent, dword wParam, dword lParam);
	dword RESET_ProcIDL(dword dwType, dword dwEvent, dword wParam, dword lParam);
	dword RESET_ProcHOME(dword dwType, dword dwEvent, dword wParam, dword lParam);
	dword RESET_ProcRESET(dword dwType, dword dwEvent, dword wParam, dword lParam);

//*****  MANUAL STATE  *****
	dword MANUAL_STATE(dword dwType, dword dwEvent, dword wParam, dword lParam);
	dword MANUAL_ProcIDL(dword dwType, dword dwEvent, dword wParam, dword lParam);
	dword MANUAL_ProcAUTO(dword dwType, dword dwEvent, dword wParam, dword lParam);
	dword MANUAL_ProcREAD(dword dwType, dword dwEvent, dword wParam, dword lParam);
		
/*===============================================
  EVENT HANDLER
===============================================*/
public:
	virtual dword Event_Handler(dword dwType, dword dwEvent, dword wParam, dword lParam);
	virtual dword HEvent_Handler(dword dwType, dword dwEvent, dword wParam, dword lParam);
	
protected:
	dword Event_PreProc(dword dwType, dword dwEvent, dword wParam, dword lParam);
	dword Event_PostProc(dword dwType, dword dwEvent, dword wParam, dword lParam);
	dword Command_Handler(dword cmd, dword wp, dword lp);
};

extern "C" void HIO_Delay(dword msec);

#endif /* _SYSMGR_H_ */


/*=============================================================================
  EXTERNALIZED FUNCTION DESCRPTIONS
=============================================================================*/
/*--- END OF SYSmgr.H ---*/
